package com.seh.contentcenter.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seh.contentcenter.dto.ReplyDTO;
import com.seh.contentcenter.dto.UserDTO;
import com.seh.contentcenter.entity.Reply;
import com.seh.contentcenter.feignclient.UserCenterFeignClient;
import com.seh.contentcenter.mapper.ReplyMapper;
import com.seh.contentcenter.service.ReplyService;
import com.seh.contentcenter.utils.DTOUtil;
import com.seh.contentcenter.utils.WrapperUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ReplyServiceImpl implements ReplyService {

    @Autowired
    private ReplyMapper replyMapper;
    @Autowired
    private UserCenterFeignClient userCenterFeignClient;
    @Autowired
    private RedisTemplate<String, Object> objectRedisTemplate;

    public static long replySize;

    /**
     * 获取某一评论的回复
     * @param kid
     * @return
     */
    @Override
    public List<ReplyDTO> getRepliesByCommentId(Integer kid, Integer currentPage) {
        Page<Reply> replyPage = new Page<>(currentPage, 2);
        QueryWrapper<Reply> queryWrapper = WrapperUtil.pageWrapper(kid, "kid", "kid");
        Page<Reply> replyPage1 = replyMapper.selectPage(replyPage, queryWrapper);
        List<Reply> replies = replyPage1.getRecords();
        replySize = replyPage.getPages();
        return convertToReplyDTOList(replies);
    }

    /**
     * 新增评论--回复楼主
     * @param kid
     * @param replyName
     * @param replyContent
     * @param pid
     * @return
     */
    @Override
    public String insertReply(Integer kid, String replyName, String replyContent, Integer pid, Integer userId) {
        Reply reply = new Reply();
        reply.setKid(kid);
        reply.setReplierName(replyName);
        reply.setReply(replyContent);
        reply.setDate(new Date());
        reply.setPrizes(0);
        reply.setPid(pid);
        reply.setUserId(userId);
        int insert = replyMapper.insert(reply);
        if (insert == 1) {
            objectRedisTemplate.delete("comments");
            objectRedisTemplate.delete("comment");
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 获取一个帖子所有楼层内的回复数
     * @param kid
     * @return
     */
    @Override
    public Integer replyNums(List<Integer> kid) {

        Integer total = 0;
        for (Integer id : kid) {
            QueryWrapper<Reply> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("kid", id);
            Integer aLong = Math.toIntExact(replyMapper.selectCount(queryWrapper));
            total += aLong;
        }
        return total;
    }

    /**
     * 将数据库的基本类转换为用于前后端数据交互的DTO类
     * @param reply
     * @return
     */
    private ReplyDTO convertToReplyDTO(Reply reply) {
        ReplyDTO replyDTO = DTOUtil.convertReply(reply);
        replyDTO.setUserPic(getUserPic(reply.getUserId()));
        return replyDTO;
    }

    private List<ReplyDTO> convertToReplyDTOList(List<Reply> replies) {
        List<ReplyDTO> replyDTOs = new ArrayList<>();
        for (Reply reply : replies) {
            replyDTOs.add(convertToReplyDTO(reply));
        }
        return replyDTOs;
    }

    /**
     * 获取用户头像地址
     * @param userId
     * @return
     */
    public String getUserPic(Integer userId) {
        UserDTO userDTO = userCenterFeignClient.findUserInfoById(userId);
        String pic = userDTO.getPic();
        return pic;
    }
}
